#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:

"""
@version: 1.0
@author: readerror
@contact: readerror@readerror.cn
@contact1: readerror000@gmail.com
@site: http://www.readerror.cn
@software: PyCharm
@file: system.py
@time: 2017/3/22 10:23
"""


class SystemApiMixin(object):
    def api_call(self, api, data, async=False, period=60.0):
        NotImplementedError()

    def get_api_result(self, res, api_name=False):
        NotImplementedError()

    def share_resource(self, data, async=False):
        """
        ShareResource
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'accountUuids': ['e443033d408741f2a435a0b5def18701'],
         'resourceUuids': ['16d77ab57d5d48d9a5c1eb0730836032'],
         'session': {'callid': 'api-PeNIbSED',
                     'uuid': '1d11ec58315048428d8fb255768b9c4f'}}
        """
        rsp = self.api_call('org.zstack.header.identity.APIShareResourceMsg', data, async=async)
        return self.get_api_result(rsp)

    def create_system_tag(self, data, async=False):
        """
        CreateSystemTag
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'resourceType': 'InstanceOfferingVO',
         'resourceUuid': 'f7d6448d35d14a09bedbc4fa7000e07c',
         'session': {'callid': 'api-ZAXOpK9x',
                     'uuid': 'f2393fbf099945d6992864b72b579462'},
         'tag': 'guestL3Network::b7e7b79ce1174b8eb9bea2b249bd5d0a'}
        """
        rsp = self.api_call('org.zstack.header.tag.APICreateSystemTagMsg', data, async=async, period=0.1)
        return self.get_api_result(rsp)

    # 查询cpu、内存
    def query_host(self, data=None, async=False):
        """
        QueryHost
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'conditions': [], 'session': {'uuid': 'ee5aa4ad6a9e4bfbb5827636e04a759d'}}
        k=org.zstack.header.host.APIQueryHostMsg
        name=QueryHost
        """
        if data is None:
            data = {}
        if data.get('conditions') is None:
            data['conditions'] = []
        rsp = self.api_call("org.zstack.header.host.APIQueryHostMsg", data, async=async)
        return self.get_api_result(rsp)

    # 查询主存储
    def query_primary_storage(self, data=None, async=False):
        """
        QueryPrimaryStorage
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'conditions': [], 'session': {'uuid': 'ee5aa4ad6a9e4bfbb5827636e04a759d'}}
        """
        if data is None:
            data = {}
        if data.get('conditions') is None:
            data['conditions'] = []
        rsp = self.api_call("org.zstack.header.storage.primary.APIQueryPrimaryStorageMsg", data, async=async)
        return self.get_api_result(rsp)

    # 查询镜像服务器
    def query_backup_storage(self, data=None, async=False):
        """
        QueryBackupStorage
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'conditions': [], 'session': {'uuid': 'ee5aa4ad6a9e4bfbb5827636e04a759d'}}
        """
        if data is None:
            data = {}
        if data.get('conditions') is None:
            data['conditions'] = []
        rsp = self.api_call("org.zstack.header.storage.backup.APIQueryBackupStorageMsg", data, async=async)
        return self.get_api_result(rsp)

    def query_account_resource_ref(self, data=None, async=False):
        """
        QueryAccountResourceRef
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'conditions': [{'name': 'accountUuid',
                         'op': '=',
                         'value': '7ad4095bd82243ed895352ec58fbfb19'}],
         'fields': ['resourceType', 'resourceUuid'],
         'session': {'uuid': 'd11f1405b35248a7bc0886a60a61e2d2'}}
        """
        if data is None:
            data = {}
        if data.get('conditions') is None:
            data['conditions'] = []
        rsp = self.api_call("org.zstack.header.identity.APIQueryAccountResourceRefMsg", data, async=async)
        return self.get_api_result(rsp)
